package socket;

import com.alibaba.fastjson.JSONObject;

public class CmdMessage {
    public static final String CMD_FIELD = "cmd";
    public static final String MESSAGE_FIELD = "message";

    private JSONObject jo = null;
    private String json = null;
    public String cmd;
    public String message;

    public CmdMessage(String cmd, String message) {
        this.cmd = cmd;
        this.message = message;
    }

    public CmdMessage(String json) {
        this.json = json;
        jo = JSONObject.parseObject(json);
        cmd = jo.getString(CMD_FIELD);
        message = jo.getString(MESSAGE_FIELD);
    }

    @Override
    public String toString() {
        if (json == null) {
            jo = new JSONObject(2);
            jo.put(CMD_FIELD, cmd);
            jo.put(MESSAGE_FIELD, message);
            json = jo.toJSONString();
        }
        return json;
    }
}
